package cn.ncupyh.soft.opera;

import cn.ncupyh.soft.book.Book;
import cn.ncupyh.soft.book.BookList;

import java.util.Scanner;

/**
 * @version 1.8
 * @Author Djx_hmbb
 * @Date 2023/7/22 9:55
 * @注释:
 */
public class DeleteOperation implements IOPeration{

    public void work(BookList bookList){
        System.out.println("删除图书!");
        System.out.println("请输入你要删除的图书名称:");
        Scanner s = new Scanner(System.in);
        String bookName = s.next();
        int currentSize = bookList.getUsedSize();
        int index = -1;
        for (int i = 0; i < currentSize; i++) {
            Book book = bookList.getBook(i);
            if(book.getName().equals(bookName)){
                index = i;
                break;
            }
        }
        if(index == -1){
            System.out.println("查无此书!");
            return;
        }
        //删除
        for (int i = index; i < currentSize-1; i++) {
            Book book = bookList.getBook(i);//得到要删除的位子
            bookList.setBook(i,book);//后面的依次往前移动
        }
        bookList.setUsedSize(currentSize-1);
        bookList.setBook(currentSize-1,null);
        System.out.println("成功删除书本信息!");
    }
}
